<?php
namespace Merchant\Controller;
use Think\Controller;
class TypeController extends CommController {
    public function index(){
        $db = M('type');
        if($_GET['selname']){
            $map[]['a.name'] = array('like','%'.$_GET['selname'].'%');
            $map[]['a.pid'] =0;
        }else{
            $map[]['a.pid'] =0;
        }


        $count = $db->alias('a')->order('concat(a.path,a.id)')
            ->join('type b ON a.pid = b.id','left')
            ->field('a.*,b.name pname')
            ->where($map)->count();
        $Page = new \Think\Page($count,4);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $db->alias('a')
            ->join('type b ON a.pid = b.id','left')
            ->field('a.*,b.name pname')
            ->order('concat(a.path,a.id)')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->where($map)->select();

        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',$list);
        $this->display();
    }
    public function edit()
    {
        $db = M('type');
        if(IS_POST)
        {
            $parent = $db->find($_POST['pid']);
            if ($_POST['pid'] == 0)
                $_POST['path'] = '0,';
            else
                $_POST['path'] = $parent['path'] . $_POST['pid'] . ',';
            $pic = $this->upload();
            if ($pic['img'])
                $_POST['img'] = $pic['img']['savepath'].$pic['img']['savename'];
            if($db->save($_POST))
            {
                alert('修改成功');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }else{
                alert('修改失败');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }
        }else{
            $blist=M("business")->select();
            $this->assign("blist",$blist);
            $this->assign('list',$this->get());
            $this->assign('info',$db->find($_GET['id']));
            $this->display();
        }
    }
       public function sel(){

         $db = M('type');
         if($_GET['selname']){
               $map[]['name'] = array('like','%'.$_GET['selname'].'%');
               $map[]['pid'] = $_GET['id'];
           }else{
             $map[]['pid'] = $_GET['id'];
         }
         $count = $db->where($map)->count();
         $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
         $show = $Page->show();// 分页显示输出
         $list = $db
             ->limit($Page->firstRow.','.$Page->listRows)
             ->order("id desc")
             ->where($map)
             ->select();

         $li = $db->where($map)->select();
         $this->assign('li',$li);

         $this->assign('list',$list);
         $st = $_GET;
         $this->assign('st',$st);
         $this->assign('page',$show);// 赋值分页输出
         $this->display();
    }

    public function seladd(){

        $db = M('type');
        if (IS_POST)
        {

            $_POST['time'] = time();
            $pic = $this->upload();
            if ($pic['img'])
                $_POST['img'] = $pic['img']['savepath'].$pic['img']['savename'];
                $parent = $db->find($_POST['pid']);
                $_POST['path'] = $parent['pid'].',' . $_GET['id'] . ',';
                $_POST['pid'] = $_GET['id'];
            if($db->data($_POST)->add())
            {
                alert('添加成功');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }else{
                alert('添加失败');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }
            return;
        }
        $list = $db->field('name')->where(['id'=>$_GET['id']])->select();
        $this->assign('list',$list);
//        $blist=M("business")->field('name')->where(['id'=>$_GET['id']])->select();
//        var_dump($blist);
//
//        $this->assign("blist",$blist);
        $this->display();

    }

    public function add()
    {
        if (IS_POST)
        {
            $db = M('type');
            $_POST['time'] = time();
            $pic = $this->upload();
            if ($pic['img'])
                $_POST['img'] = $pic['img']['savepath'].$pic['img']['savename'];
            $parent = $db->find($_POST['pid']);
            if ($_POST['pid'] == 0)
                $_POST['path'] = '0,';
            else
                $_POST['path'] = $parent['path'] . $_POST['pid'] . ',';
            if($db->data($_POST)->add())
            {
                alert('添加成功');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }else{
                alert('添加失败');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }
            return;
        }
//        $blist=M("business")->select();
//        $this->assign("blist",$blist);
        $this->assign('list',$this->get(0));
        $this->display();
    }
    public function get($pid = '')
    {
        $where = '';
        if($pid != '') $where['pid'] = $pid;
        $list = M('type')->order('concat(path,id)')->where($where)->select();
        return $list;
    }
    public function qj()
    {
        $db = M('type');
        $info = $db->find($_POST['id']);
        if (!$info){
            echo "{'state':3,'message':'没有这条数据'}";
            return;
        }
        if($info['state'] == 1)
        {
            $_POST['state'] = 2;
            if (!$db->save($_POST))
                echo json_encode(['state'=>'3','message'=>'修改失败']);
            else
                echo json_encode(['state'=>'2','message'=>'修改成功']);
        }else{
            $_POST['state'] = 1;
            if (!$db->save($_POST))
                echo json_encode(['state'=>'3','message'=>'修改失败']);
            else
                echo json_encode(['state'=>'1','message'=>'修改成功']);
        }
    }
    public function del()
    {
        if (!$_GET['id']) return ;
        $db = M('type');

        if($db->where(['pid'=>$_GET['id']])->select())
        {
            alert("该分类下面有子集 请先删除再试");
            echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            die;

        }
        $info = $db->find($_GET['id']);
        unlink('.'.$info['img']);
        alert("删除后不可恢复！");
        if($db->delete($_GET['id']))
        {
             alert('删除成功');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
        }else{
            alert('删除失败');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
        }
    }
}